package com.car.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.commons.core.model.Result;
import com.car.serve.Param.addServeProjectParam;
import com.car.serve.Param.upServeProjectParam;
import com.car.serve.client.OrderClient;
import com.car.serve.client.UserClient;
import com.car.serve.client.dto.AdminDto;
import com.car.serve.client.form.StockInsertForm;
import com.car.serve.entity.InfoProject;
import com.car.serve.entity.ServeInfo;
import com.car.serve.entity.ServeProject;
import com.car.serve.entity.ServeProjectModifyLog;
import com.car.serve.exception.ServeException;
import com.car.serve.form.PageServeProjectForm;
import com.car.serve.mapper.InfoProjectMapper;
import com.car.serve.mapper.ServeInfoMapper;
import com.car.serve.mapper.ServeProjectMapper;
import com.car.serve.service.InfoProjectService;
import com.car.serve.service.ServeInfoService;
import com.car.serve.service.ServeProjectModifyLogService;
import com.car.serve.service.ServeProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class ServeProjectServiceImpl extends ServiceImpl<ServeProjectMapper, ServeProject> implements ServeProjectService {

    @Resource
    private ServeProjectMapper serveProjectMapper;
    @Resource
    private ServeProjectModifyLogService serveProjectModifyLogService;
    @Resource
    private InfoProjectMapper infoProjectMapper;
    @Resource
    private OrderClient orderClient;
    @Resource
    private InfoProjectService infoProjectService;
    @Resource
    private ServeInfoService serveInfoService;
    @Resource
    private ServeInfoMapper serveInfoMapper;
    @Resource
    private UserClient userClient;
    @Override
    public void addServeProject(addServeProjectParam param) throws ServeException {
        //根据条件查询项目名是否存在
        QueryWrapper<ServeProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_name",param.getProjectName());
        ServeProject serveProject = serveProjectMapper.selectOne(queryWrapper);
        if (serveProject!=null)throw new ServeException(3000,"服务项目名已存在");
        //新增服务名
        ServeProject serveProject1 = BeanUtil.toBean(param, ServeProject.class);
        serveProject1.setProjectName(param.getProjectName());
        serveProject1.setProjectDetail(param.getProjectDetail());
        serveProject1.setServeProjectState(0);
        serveProject1.setProjectPrice(param.getProjectPrice());
        serveProject1.setImage(param.getImage());
        serveProjectMapper.insert(serveProject1);
        //新增所有门店库存
        StockInsertForm stockInsertForm = new StockInsertForm();
        stockInsertForm.setUnit(param.getUnit());
        stockInsertForm.setServeProjectId(serveProject1.getProjectId());
        orderClient.insert(stockInsertForm);


    }

    @Override
    public void upServeProject(upServeProjectParam param) throws Exception {
        //查找项目是否存在
        ServeProject serveProject = serveProjectMapper.selectById(param.getProjectId());
        if (serveProject==null)throw new ServeException(3000,"项目不存在");
        //修改前的价格1
        BigDecimal projectPrice = serveProject.getProjectPrice();
        //修改前的价格2
        BigDecimal projectPrice2 = serveProject.getProjectPrice();
        //判断该项目是否可用
        if (serveProject.getServeProjectState()!=0)throw new ServeException(3000,"当前项目不可用");
        //修改项目金额
        serveProject.setProjectPrice(param.getProjectPrice());
        //获取当前系统时间并修改
        String createTime = DateUtil.format(LocalDateTime.now(),"yyyy-MM-dd HH:mm:ss");
        serveProject.setModifyTime(createTime);
        //增加修改人
        serveProject.setAdminId(param.getAdminId());
        Result<AdminDto> adminDtoResult = userClient.selectAdminDto(param.getAdminId());
        String adminName = adminDtoResult.getData().getAdminName();
        serveProject.setAdminName(adminName);
        serveProjectMapper.updateById(serveProject);
        //修改服务价格
        //根据项目ID去查询跟该项目相关的关联表
        List<InfoProject> infoProjectList = infoProjectService.list(new QueryWrapper<InfoProject>()
                .eq("project_id", param.getProjectId()));
        //修改后的价格
        BigDecimal projectPrice1 = serveProject.getProjectPrice();
        //修改后的价格减去修改前的价格
        BigDecimal money = projectPrice1.subtract(projectPrice);
        for (InfoProject infoProject : infoProjectList) {
            ServeInfo serveInfo = serveInfoService.getById(infoProject.getServeId());
            serveInfo.setServePrice(serveInfo.getServePrice().add(money));
            serveInfoMapper.updateById(serveInfo);
        }
        //新增项目修改日志
        ServeProjectModifyLog serveProjectModifyLog = new ServeProjectModifyLog();
        serveProjectModifyLog.setAdminId(param.getAdminId());
        serveProjectModifyLog.setServeProjectId(serveProject.getProjectId());
        serveProjectModifyLog.setModifyTime(createTime);
        serveProjectModifyLog.setReviseFormer(projectPrice2);
        serveProjectModifyLog.setReviseAfter(serveProject.getProjectPrice());
        serveProjectModifyLog.setProjectName(serveProject.getProjectName());
        serveProjectModifyLog.setLogInfo("项目金额修改成功");
        serveProjectModifyLogService.save(serveProjectModifyLog);

    }

    @Override
    public void delServeProject(Integer projectId) throws Exception {
        //根据ID查询项目
        ServeProject serveProject = serveProjectMapper.selectById(projectId);
        if (serveProject==null)throw new ServeException(3000,"该服务不存在");
        if (serveProject.getServeProjectState()==2)throw new ServeException(3000,"该服务已被删除");
        //根据条件更新状态为删除
        UpdateWrapper<ServeProject> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("serve_project_state",2);
        updateWrapper.eq("project_id",serveProject.getProjectId());
        serveProjectMapper.update(null,updateWrapper);
    }

    @Override
    public void disableServeProject(Integer projectId) throws Exception {
        //根据ID查询项目
        ServeProject serveProject = serveProjectMapper.selectById(projectId);
        if (serveProject==null)throw new ServeException(3000,"该服务不存在");
        if (serveProject.getServeProjectState()!=0)throw new ServeException(3000,"该服务已被删除或禁用");
        UpdateWrapper<ServeProject> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("serve_project_state",1);
        updateWrapper.eq("project_id",serveProject.getProjectId());
        serveProjectMapper.update(null,updateWrapper);
    }

    @Override
    public void enableServeProject(Integer projectId) throws Exception {
        //根据ID查询项目
        ServeProject serveProject = serveProjectMapper.selectById(projectId);
        if (serveProject==null)throw new ServeException(3000,"该服务不存在");
        if (serveProject.getServeProjectState()!=1)throw new ServeException(3000,"该服务已被删除或启用");
        UpdateWrapper<ServeProject> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("serve_project_state",0);
        updateWrapper.eq("project_id",serveProject.getProjectId());
        serveProjectMapper.update(null,updateWrapper);
    }

    @Override
    public Page<ServeProject> findPageServeProject(PageServeProjectForm form) {
        Page<ServeProject> page = new Page<>(form.getPageNum() == null ? 1 : form.getPageNum(), form.getPageSize() == null ? 6 : form.getPageSize());
        QueryWrapper<ServeProject> wrapper = new QueryWrapper<>();
        //判断服务名是否为空
        if (ObjectUtil.isNotEmpty(form.getProjectName())) {
            wrapper.like("project_name", form.getProjectName());
        }
        // 只展示状态为 0 的记录
        wrapper.eq("serve_project_state", 0);
        Page<ServeProject> serveProjectPage = serveProjectMapper.selectPage(page, wrapper);
        return serveProjectPage;
    }
}
